<?php

/**
 * imports graphs from files
 */

require_once(dirname(__FILE__) . '/include.php');

$dirPath = 'graphs/';
$directory = opendir($dirPath);
if ($directory !== false) {
	while($item = readdir($directory)) {
		if(($item != ".") && ($item != "..") && ($item != ".svn")) {
			$filename = $dirPath . $item;
			$file = fopen($filename, 'r');
			$content = fread($file, filesize($filename));
			$lines = explode("\n", $content);

			$matrix = array();
			foreach ($lines as $line) {
				if (strpos($line, 'c') === 0) {
					continue;
				}
				if (strpos($line, 'p') === 0) {
					$definitions = explode(' ', $line);
					continue;
				}
				if (strpos($line, 'e') === 0) {
					$edge = explode(' ', trim($line));
					$matrix[$edge[1]][$edge[2]] = 1;
					$matrix[$edge[2]][$edge[1]] = 1;
				}
			}

			$pathinfo = pathinfo($filename);
			
			$params = array(
				'matrix' => serialize($matrix),
				'number_of_vertices' => $definitions[2],
				'number_of_edges' => $definitions[3],
				'title' => $pathinfo['filename'],
			);

			$GLOBALS['db']->insert('graph', $params);
		}
	}
}
closedir($directory);

echo "DONE";

?>